﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.SalePerformance.Record;
using Common.Contract.SalePerformance.Request;
using Common.Contract.SalePerformance.Response;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Core.ServiceCommand
{
    public class RetrieveRankingSaleContestBranchServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveRankingSaleContestMSBORequest, RetrieveRankingSaleContestMSBOResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string Branch;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public int SaleContestID;
        #endregion
        public RetrieveRankingSaleContestBranchServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveRankingSaleContestMSBORequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = request.CurrentUser;
            }
            if (string.IsNullOrEmpty(request.SaleProgramId) && Int32.TryParse(request.SaleProgramId, out SaleContestID))
            {
                throw new ArgumentException("Sale contest must be set", "request");
            }
            else
            {
                SaleContestID = Int32.Parse(request.SaleProgramId);
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }
        protected override bool OnExecute(RetrieveRankingSaleContestMSBOResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<RankingSaleContestMSBORecord> records = new List<RankingSaleContestMSBORecord>();
                List<TblSaleContestBilawardRankingDaily> listLoans = new List<TblSaleContestBilawardRankingDaily>();
                string regionGroup = Providers.SalePerformance.RetrieveSaleContestRankingBranchRegionGroup(Dao, SaleContestID);
                listLoans = Providers.SalePerformance.RetrieveSaleContestRankingBranchByDao(SaleContestID);
                if (listLoans != null)
                {
                    switch (Dao.GroupIdentifier)
                    {
                        case "1":
                            records = listLoans.Select(x => new RankingSaleContestMSBORecord
                            {
                                BranchNameSme = x.BranchNameSme,
                                GroupRegion = x.GroupRegion,
                                XepHang = x.XepHang,
                                XetthuongYn = x.XetthuongYn,
                                NetDuno = x.NetDuno,
                                Region = x.Region
                            }).ToList();
                            break;
                        case "2":
                            records = listLoans.Where(x => x.GroupRegion.Equals(regionGroup)).Take(3).Select(x => new RankingSaleContestMSBORecord
                            {
                                BranchNameSme = x.BranchNameSme,
                                GroupRegion = x.GroupRegion,
                                XepHang = x.XepHang,
                                XetthuongYn = x.XetthuongYn,
                                NetDuno = x.NetDuno
                            }).ToList();
                            records.Add(new RankingSaleContestMSBORecord
                            {
                                XepHang = -1,
                            });
                            records.AddRange(listLoans.Where(x => x.GroupRegion.Equals(regionGroup)).Skip(3).Where(x => x.Region == Dao.Zone && x.XepHang > 3).Select(x => new RankingSaleContestMSBORecord
                            {
                                BranchNameSme = x.BranchNameSme,
                                GroupRegion = x.GroupRegion,
                                XepHang = x.XepHang,
                                XetthuongYn = x.XetthuongYn,
                                NetDuno = x.NetDuno
                            }).ToList());
                            break;
                        case "3":
                            records = listLoans.Take(3).Select(x => new RankingSaleContestMSBORecord
                            {
                                BranchNameSme = x.BranchNameSme,
                                GroupRegion = x.GroupRegion,
                                XepHang = x.XepHang,
                                XetthuongYn = x.XetthuongYn,
                                NetDuno = x.NetDuno
                            }).ToList();
                            if (listLoans.Where(x => x.BranchCodeSme == Dao.BranchCode).FirstOrDefault().XepHang > 3)
                            {
                                records.Add(new RankingSaleContestMSBORecord
                                {
                                    XepHang = -1,
                                });
                                records.AddRange(listLoans.Skip(3).Where(x => x.BranchCodeSme == Dao.BranchCode && x.XepHang > 3).Select(x => new RankingSaleContestMSBORecord
                                {
                                    BranchNameSme = x.BranchNameSme,
                                    GroupRegion = x.GroupRegion,
                                    XepHang = x.XepHang,
                                    XetthuongYn = x.XetthuongYn,
                                    NetDuno = x.NetDuno
                                }).ToList());
                            }
                            break;
                        default:
                            break;
                    }
                }
                totalRecord = records.Count();
                if (pageSize > 0 && pageIndex >= 0)
                {
                    records = records.Skip(pageIndex * pageSize).Take(pageSize).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
